package util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.copier.Copier;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import stream.User;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

@Data
@ApiModel("游标翻页返回")
@AllArgsConstructor
@NoArgsConstructor
public class CursorPageBaseResp<T> {

    @ApiModelProperty("游标（下次翻页带上这参数）")
    private String cursor;

    @ApiModelProperty("是否最后一页")
    private Boolean isLast = Boolean.FALSE;

    @ApiModelProperty("数据列表")
    private List<T> list;

    public static void main(String[] args) throws NoSuchMethodException {

        User user = new User("1",2,3);
        SFunction<User,Integer> getAge = User::getAge;
        getType(getAge);
        System.out.println(100/1e9);
    }

    public static <T> void getType(SFunction<T, ?> cursorColumn) throws NoSuchMethodException {
        Type[] superClass =  cursorColumn.getClass().getGenericInterfaces();
        Class<?> instantiatedClass = LambdaUtils.extract(cursorColumn).getInstantiatedClass();
        Type genericType = cursorColumn.getClass().getGenericInterfaces()[0];
      //  ParameterizedType paramType = (ParameterizedType) genericType;
        Method method = cursorColumn.getClass().getDeclaredMethod("writeReplace");
        method.setAccessible(true);
        AnnotatedType annotatedReturnType = method.getAnnotatedReturnType();
//        Class<?> returnType = method.getGenericReturnType();
        System.out.println(annotatedReturnType);
//        System.out.println(paramType.getActualTypeArguments()[1]);
        System.err.println(instantiatedClass);

        }
    }

//    public static  <T> CursorPageBaseResp<T> getCursorPageByMysql(IService<T> mapper, CursorPageBaseReq request, Consumer<LambdaQueryWrapper<T>> initWrapper, SFunction<T, ?> cursorColumn) {
//        //游标字段类型
//
//        Class<?> cursorType = LambdaUtils.getReturnType(cursorColumn);
//        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
//        //给查询器初始化额外条件
//        initWrapper.accept(wrapper);
//        //游标条件
//        if (StrUtil.isNotBlank(request.getCursor())) {
//            wrapper.lt(cursorColumn, request.getCursor());
//        }
//        //游标方向
//        wrapper.orderByDesc(cursorColumn);
//
//        Page<T> page = mapper.page(request.plusPage(), wrapper);
//        //取出游标
//        String cursor = Optional.ofNullable(CollectionUtil.getLast(page.getRecords()))
//                .map(cursorColumn)
//                .map((i)->String.valueOf(i))
//                .orElse(null);
//        //判断是否最后一页
//        Boolean isLast = page.getRecords().size() != request.getPageSize();
//        return new CursorPageBaseResp<>(cursor, isLast, page.getRecords());
//    }




